<template>
  <div class="mod-sys__orders">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-date-picker v-model="state.datetimerange" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss" :range-separator="$t('datePicker.range')" :start-placeholder="$t('datePicker.start')" :end-placeholder="$t('datePicker.end')"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="state.datetimerange" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss" :range-separator="$t('datePicker.range')" :start-placeholder="$t('datePicker.start')" :end-placeholder="$t('datePicker.end')"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.payMethod" placeholder="支付方式" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.payStatus" placeholder="支付状态" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:orders:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:orders:delete')" type="danger" @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="statusDescription" label="订单状态" header-align="center" align="center"></el-table-column>
      <el-table-column prop="userId" label="用户ID" header-align="center" align="center"></el-table-column>
      <el-table-column prop="deliveryAddress" label="配送地址" header-align="center" align="center"></el-table-column>
      <el-table-column prop="orderTime" label="下单时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="checkoutTime" label="支付完成时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="payMethod" label="支付方式" header-align="center" align="center"></el-table-column>
      <el-table-column prop="payStatus" label="支付状态" header-align="center" align="center"></el-table-column>
      <el-table-column prop="amount" label="订单金额" header-align="center" align="center"></el-table-column>
      <el-table-column prop="remark" label="订单备注" header-align="center" align="center"></el-table-column>
      <el-table-column prop="deliveryTime" label="预计送达时间" header-align="center" align="center"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('sys:orders:update')" type="primary" link @click="addOrUpdateHandle(scope.row.orderId)">{{ $t("update") }}</el-button>
          <el-button v-if="state.hasPermission('sys:orders:delete')" type="primary" link @click="state.deleteHandle(scope.row.orderId)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import AddOrUpdate from "./orders-add-or-update.vue";

const view = reactive({
  getDataListURL: "/sys/orders/page",
  getDataListIsPage: true,
  exportURL: "/sys/orders/export",
  deleteURL: "/sys/orders",
  deleteIsBatch: true,
  datetimerange: null,
  datetimerange: null,
  dataForm: {
    startDateTime: null as number | null,
    endDateTime: null as number | null,
    startDateTime: null as number | null,
    endDateTime: null as number | null,
    payMethod: "",
    payStatus: "",
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

watch(
  () => state.datetimerange,
  (val: number[] | null) => {
    if (val === null) {
      state.dataForm.startDateTime = null;
      state.dataForm.endDateTime = null;
    } else {
      state.dataForm.startDateTime = val[0];
      state.dataForm.endDateTime = val[1];
    }
  }
);
watch(
  () => state.datetimerange,
  (val: number[] | null) => {
    if (val === null) {
      state.dataForm.startDateTime = null;
      state.dataForm.endDateTime = null;
    } else {
      state.dataForm.startDateTime = val[0];
      state.dataForm.endDateTime = val[1];
    }
  }
);

const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (orderId?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(orderId);
  });
};
</script>